package org.example.Helloworl.service;

import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.example.Helloworl.config.SystemConfig;
import org.example.Helloworl.model.Result;
import org.example.Helloworl.utils.WeBASEUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WeBASEServiceImpl {

    @Autowired
    WeBASEUtils weBASEUtils;

    @Autowired
    SystemConfig systemConfig;

    public static final String ABI = org.example.Helloworl.utils.IOUtil.readResourceAsString("abi/HelloWorld.abi");

    public Result<String> setImpl(String data) {
        List funcParam = new ArrayList();
        funcParam.add(data);
        Dict result = weBASEUtils.commonReq(systemConfig.getAdminAddress(),
                "set", funcParam, ABI, "HelloWorld", systemConfig.getContract().getContractAddress());
        JSONObject respBody = JSONUtil.parseObj(result.get("result"));
        return Result.success("ok");
    }

    public Result<String> getImpl() {
        List funcParam = new ArrayList();
        Dict result =weBASEUtils.commonReq(systemConfig.getAdminAddress(),
                "get", funcParam, ABI, "HelloWorld", systemConfig.getContract().getContractAddress());
        JSONArray res = JSONUtil.parseArray(result.get("result"));
        return Result.success((String)res.get(0));
    }

}
